package zelix.gui.clickguis.kendall;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import zelix.Core;
import zelix.gui.clickguis.caesium.Panel;
import zelix.gui.clickguis.kendall.components.Component;
import zelix.gui.clickguis.kendall.components.impls.*;
import zelix.gui.clickguis.kendall.frame.KendallFrame;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.hack.hacks.ClickGui;
import zelix.utils.Wrapper;
import zelix.utils.hooks.visual.ColorUtils;
import zelix.utils.hooks.visual.font.FontLoaders;
import zelix.utils.tenacityutils.render.ColorUtil;
import zelix.utils.tenacityutils.render.RenderUtil;
import zelix.utils.tenacityutils.render.ShaderUtil;
import zelix.utils.visual.RenderUtils;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;

public class KendallScreen extends GuiScreen {

    public ArrayList<KendallFrame> frames = new ArrayList<KendallFrame>();


    public boolean onMoving=false;
    public ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
    public double x = 0;
    public double y = 0;

    public boolean ListOpen=true;

    public float moveX = 0, moveY = 0;
    private final FontRenderer fr = Wrapper.INSTANCE.fontRenderer();
    protected ArrayList<KendallFrame> getFrames() {
        return frames;
    }

    protected void addFrame(KendallFrame frame) {
        if (!frames.contains(frame)) {
            frames.add(frame);
        }
    }

    public KendallScreen(){
        int x = 20;

        for (HackCategory category: HackCategory.values()) {
            addFrame(new KendallFrame(category,x,20));
            x += 110;
        }

    }

    @Override
    public void initGui() {
        for (KendallFrame frame : frames) {
           frame.initialize();
        }
    }

    @Override
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
        if (isHovered((float) x, (float) y, (float) (x+width), (float) (y+30), mouseX, mouseY) && mouseButton ==0){
            onMoving = true;
        }
        if(isHovered((float) x, (float) y, (float) (x+width), (float) (y+30), mouseX, mouseY) && mouseButton ==1){
            ListOpen=!ListOpen;
        }
        if(this.isVIsableSW && this.targetbt != null){
            for (Component cp: targetbt.components) {
                cp.mouseClicked(mouseX, mouseY, mouseButton);
            }
            return;
        }

        for (KendallFrame frame : frames) {

            frame.mouseClicked(mouseX, mouseY, mouseButton);
        }

    }
    @Override
    protected void mouseReleased(int mouseX, int mouseY, int state) {
        onMoving = false;
        for (KendallFrame frame : frames) {

            frame.mouseReleased(mouseX, mouseY, state);

        }
    }
    @Override
    protected void keyTyped(char typedChar, int keyCode) throws IOException {

        if (isVIsableSW){
            if (this.targetbt.kendallKeyBind.keybingding){
                this.targetbt.kendallKeyBind.mod.setKey(keyCode);
                this.targetbt.kendallKeyBind.keybingding = false;
                return;
            }
        }
        if (isVIsableSW && keyCode == Keyboard.KEY_ESCAPE){
            isVIsableSW = false;
            return;
        }
        super.keyTyped(typedChar, keyCode);
        for (KendallFrame frame : frames){
            frame.keyTyped(typedChar,keyCode);
        }
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        ScaledResolution sR = new ScaledResolution(mc);
        for (KendallFrame frame : frames) {
            frame.render(mouseX, mouseY);
        }
        if(ClickGui.KendallMyGod.isVIsableSW){
            isVIsableSW = false;
            this.ShowSettingsWindow(mouseX,mouseY);
        }

    }

    @Override
    public void onGuiClosed() {
        super.onGuiClosed();


        mc.entityRenderer.stopUseShader();
        Core.fileManager.saveClickGui_Kendall();
        Core.fileManager.saveHacks();

    }


    public boolean isVIsableSW = false;
    public KendallButton targetbt = null;
    public void ShowSettingsWindow(int mouseX, int mouseY){
        if (targetbt == null){
            isVIsableSW = false;
            return;
        }
        isVIsableSW = true;


        scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
        double width = 500/2;
        double height = 82/2+58/2*(targetbt.components.size()-1)-2;
        if(!ListOpen){
            height=68/2-2;
        }



        if((x<=0 && y<=0)){
              x = scaledResolution.getScaledWidth()/2 - width/2;
              y = scaledResolution.getScaledHeight()/2 - height/2;
        }



        if (isHovered((float) x, (float) y, (float) (x+width), (float) (y+30), mouseX, mouseY) && Mouse.isButtonDown(0) && onMoving) {

            if (moveX == 0 && moveY == 0) {
                moveX = (float) (mouseX -  this.x);
                moveY = (float) (mouseY - this.y);
            } else {
                this.x = (int) (mouseX - moveX);
                this.y = (int) (mouseY - moveY);
                float y2 = (float) (y+20);
            }
            //this.previousmouse = true;
        } else if (moveX != 0 || moveY != 0) {
            moveX = 0;
            moveY = 0;
        }




        //绘制上下部分，保证圆角并无锯齿


        float alphaAnimation =1;

        zelix.utils.tenacityutils.render.RenderUtil.drawRect2((double)x,(double)y,(double)width,(double) height,ColorUtil.applyOpacity(new Color(10, 10, 10), (float) (0.80 * alphaAnimation)).getRGB());
        //RenderUtil.drawRoundRect_up(x,y,x+width,y+height/2,2,0xFF1F2022);
        //RenderUtil.drawRoundRect_down(x,y+height/2,x+width,y+height,2,0xFF1F2022);

        if (!ClickGui.language.getMode("Chinese").isToggled()){
            FontLoaders.default18.drawString(targetbt.mod.getRenderName(),(float)x+20/2f,(float)y+24/2f,0xFFF5F5F7);
        }else{
            Wrapper.INSTANCE.fontRenderer().drawString(targetbt.mod.getRenderName(), (int) ((float)x+20/2f), (int) ((float)y+20/2f),0xFFF5F5F7);
        }

        String OpenIcon = this.ListOpen ? "▼" : "▶";
        Minecraft.getMinecraft().fontRenderer.drawString(OpenIcon,(int)(x+width-20),(int)(y+24/2f),0xFFF5F5F7);

        if(ListOpen){
            float ssl = (float) x ;
            float hqc = (float) y + 48/2;

            float lis = 30/2;
            for (Component sjh:targetbt.components) {

                if (sjh instanceof KendallMode && ListOpen==true){
                    KendallMode fuckssl =  (KendallMode)sjh;
                    fuckssl.render(ssl,hqc+lis,mouseX,mouseY);
                }
                if (sjh instanceof KendallSIlder && ListOpen==true){
                    KendallSIlder fuckrcx =  (KendallSIlder)sjh;
                    fuckrcx.render(ssl,hqc+lis,mouseX,mouseY);

                }
                if (sjh instanceof KendallOption && ListOpen==true){
                    KendallOption fuckhqc =  (KendallOption)sjh;
                    fuckhqc.render(ssl,hqc+lis,mouseX,mouseY);
                }
                if (sjh instanceof KendallKeyBind && ListOpen==true){
                    KendallKeyBind fuckqbg =  (KendallKeyBind)sjh;
                    fuckqbg.render(ssl,hqc,mouseX,mouseY);
                }else{
                    hqc += 58/2;
                }

            }








            //KEY
        }



    }
    public static boolean isHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
        if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
            return true;
        }

        return false;
    }


    private ResourceLocation res = new ResourceLocation("blur.png");
    public void renderBLUR( int x, int y, int width, int height){


        RenderUtils.drawImage(res,x,y,width,height);



    }


}
